﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.PartyRepresentative
{
    public partial class MeetingWithDetail : PortalPage
    {
        /// <summary>
        /// 约见ID
        /// </summary>
        private string WithID
        {
            get
            {
                return this.Request["id"] ?? "null";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            var dt = PartyRepresentativeMeetingWith.GetOne(WithID).Tables[0];
            
            if (dt.Rows.Count < 1)
            {
                Response.Redirect("~/default.aspx");
            }

           
            txtTitle.Text = Convert.ToString(dt.Rows[0]["Title"]);
            txtUserName.Text = ControlBusiness.GetMembersPickerText(Convert.ToString(dt.Rows[0]["WithBy"]));
            txtMobileNo.Text =  Convert.ToString(dt.Rows[0]["MobileNo"]);
            txtMobileNo.Text = StringHelper.MobileNoHide(txtMobileNo.Text);
            ddlRepresentativeID.Text= Convert.ToString(dt.Rows[0]["RepresentativeName"]);
            txtStudioName.Text= string.Format("<a href='StudioInfo.aspx?StudioID={0}'>{1}</a>", Convert.ToString(dt.Rows[0]["StudioID"]), Convert.ToString(dt.Rows[0]["StudioName"]));
            txtContent.Text= Convert.ToString(dt.Rows[0]["Content"]);
            litStatus.Text = Convert.ToString(dt.Rows[0]["StatusName"]);
            if (Convert.ToString(dt.Rows[0]["Status"]) != "2" || 
                string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["MeetingID"])))
            {
                return;
            }
            Macmeeting.Visible = true;
            var meetting = DB.PartyRepresentativeMeetings.Find(Convert.ToString(dt.Rows[0]["MeetingID"]));
            if (meetting == null)
            {
                Macmeeting.Visible = false;
                return;
            }
            
            litTitle.Text = meetting.Title;
            litStudioName.Text = string.Format("<a href='StudioInfo.aspx?StudioID={0}'>{1}</a>", meetting.StudioID, Convert.ToString(dt.Rows[0]["StudioName"]));
            litRepresentatives.Text = GetPartyRepresentativeJoin(Convert.ToString(dt.Rows[0]["MeetingID"]));
            litTime.Text = Convert.ToDateTime(meetting.BeginTime).ToString("yyyy-MM-dd HH:mm") + " 至 " + Convert.ToDateTime(meetting.EndTime).ToString("yyyy-MM-dd HH:mm");
            litAddress.Text = meetting.Address;
            litContent.Text = meetting.Content;

        }

        public string GetPartyRepresentativeJoin(string meetingId)
        {
            var q = DB.PartyRepresentativeMeetingJoins.Where(a => a.MeetingID == meetingId);
            var names = new List<string>();
            foreach (var item in q)
            {
                var join = DB.PartyRepresentativeInfos.Find(item.RepresentativeID);
                if (join != null)
                {
                    names.Add(join.UserName);
                }
            }
            return string.Join(",", names.ToArray());
        }
    }
}